Send Telegram Alerts for New WooCommerce Orders

工作流概述

这是一个包含6个节点的中等工作流,主要用于自动化处理各种任务。

工作流源代码

下载
{
  "id": "JMfwq2Xn60pWz2Hy",
  "meta": {
    "instanceId": "2e75c9fb3cdcf631da470c0180f0739986baa0ee860de53281e9edc3491b82a3",
    "templateCredsSetupCompleted": true
  },
  "name": "Send Telegram Alerts for New WooCommerce Orders",
  "tags": [],
  "nodes": [
    {
      "id": "bc66fcc7-55d4-46b3-929a-6e4359733601",
      "name": "Check if Order Status is Processing",
      "type": "n8n-nodes-base.if",
      "position": [
        260,
        760
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 1,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "0509abb0-c655-49de-8f2c-c4649b478983",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.body.status }}",
              "rightValue": "processing"
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "99ecb702-0264-4aeb-8b15-4383b97bc5ee",
      "name": "Design Message Template",
      "type": "n8n-nodes-base.code",
      "position": [
        500,
        740
      ],
      "parameters": {
        "jsCode": "// Data extraction and processing for order details
const lineItems = $json.body.line_items;

// Getting the total amount directly from WooCommerce
const totalAmount = parseInt($json.body.total).toLocaleString();

// Constructing the product message in the desired format
const filteredItems = lineItems.map(item => {
  const name = item.name;
  const quantity = item.quantity;
  return `🔹 ${name}<b> (${quantity} items)</b>`;
}).join('\n');  // Separating each product with a new line

// Getting the order creation date and time
let dateCreated = new Date($json.body.date_created_gmt || new Date());

// Directly using the server's local time (no timezone conversion)
let formattedDate = dateCreated.toLocaleString('en-US', {
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  hour: '2-digit',
  minute: '2-digit',
  hour12: false
});

// Constructing other parts of the message in an organized manner
const orderInfo = `

🆔 <b>Order ID:</b> ${$json.body.id}

👦🏻 <b>Customer Name:</b> ${$json.body.billing.first_name} ${$json.body.billing.last_name}

💵 <b>Amount:</b> ${totalAmount}

📅 <b>Order Date:</b>
➖ ${formattedDate}

🏙 <b>City:</b> ${$json.body.billing.city}

📞 <b>Phone:</b> ${$json.body.billing.phone}

✍🏻 <b>Order Note:</b>
${$json.body.customer_note || 'No notes'}

📦 <b>Ordered Products:</b>

${filteredItems}
`;

// Returning the final message
return {
  orderMessage: orderInfo.trim()  // Remove extra spaces from the beginning and end of the message
};"
      },
      "typeVersion": 2
    },
    {
      "id": "c2c49759-5309-42bc-872d-7b34faf34f62",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1120,
        540
      ],
      "parameters": {
        "color": 3,
        "width": 1035.4009739750634,
        "height": 868.2813781621796,
        "content": "## ⚙️ Setup Instructions

### 1. 🛒 **Configure WooCommerce Webhook**
- Navigate to **WooCommerce ➡️ Settings ➡️ Advanced ➡️ Webhooks** in your WordPress dashboard.
- Click on ➕ **Add Webhook**.
- Set the **Status** to **Active**.
- Choose **Topic**: **Order updated**.
- Paste the **Webhook URL** from the 🔗 Webhook node in this workflow into the **Delivery URL** field.
- Click 💾 **Save Webhook**.

### 2. 🤖 **Create a Telegram Bot**
- Open **Telegram** and start a chat with **@BotFather**.
- Send the command **/newbot** and follow the instructions to create your bot.
- Copy the **API Token** provided by **BotFather**.

### 3. 🔑 **Set Up Telegram Credentials in n8n**
- In **n8n**, go to **Credentials**.
- Click ➕ **Create** and select **Telegram Bot**.
- Paste the **API Token** you copied earlier.
- **Save** the credentials.

### 4. ✏️ **Configure the Telegram Node**
- Open the 📨 **Send Order Notification to Telegram** node.
- Select your **Telegram credentials**.
- Enter your **Chat ID** where you want to receive notifications.  
  **Tip**: Use **@userinfobot** in Telegram to find your **Chat ID**.

### 5. 🚀 **Activate and Test the Workflow**
- Ensure the workflow is 🟢 **Active**.
- Place a new order in your **WooCommerce store**.
- Update the order status to **\"Processing\"**.
- You should receive a **Telegram notification** with the **order details**!

## 💡 Notes
- **Customize the message format** in the 🖋️ **Design Message Template** node to include additional order details if needed.
"
      },
      "typeVersion": 1
    },
    {
      "id": "5555e7ff-46d9-4b91-a42c-4d83fc9b5edb",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1120,
        300
      ],
      "parameters": {
        "color": 5,
        "width": 1040.2541837971148,
        "height": 216.11554963705538,
        "content": "## 📦 Send Telegram Alerts for New WooCommerce Orders

📝 **Description**  
This workflow automatically sends a **Telegram notification** whenever a **WooCommerce order** status is updated to \"Processing\". It's perfect for **online store owners** who want instant updates when orders are ready to be fulfilled.
"
      },
      "typeVersion": 1
    },
    {
      "id": "acde9b85-4ae7-462f-91c0-13a4209fb013",
      "name": "Receive WooCommerce Order",
      "type": "n8n-nodes-base.webhook",
      "position": [
        20,
        760
      ],
      "webhookId": "9aeff297-db6b-4c69-93bf-21b194ef115c",
      "parameters": {
        "path": "9aeff297-db6b-4c69-93bf-21b194ef115c",
        "options": {},
        "httpMethod": "POST"
      },
      "typeVersion": 2
    },
    {
      "id": "5605e14d-a125-41c1-b7e8-cc1feeb6a1e1",
      "name": "Telegram",
      "type": "n8n-nodes-base.telegram",
      "position": [
        720,
        740
      ],
      "parameters": {
        "text": "{{ $json.orderMessage }}",
        "chatId": "<Your-Chat-ID>",
        "additionalFields": {
          "parse_mode": "HTML",
          "appendAttribution": true
        }
      },
      "typeVersion": 1.2
    }
  ],
  "active": true,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "f1a12e0e-e2a2-4eea-b7a6-cc4c7439bef9",
  "connections": {
    "Design Message Template": {
      "main": [
        [
          {
            "node": "Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Receive WooCommerce Order": {
      "main": [
        [
          {
            "node": "Check if Order Status is Processing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check if Order Status is Processing": {
      "main": [
        [
          {
            "node": "Design Message Template",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

功能特点

  • 自动检测新邮件
  • AI智能内容分析
  • 自定义分类规则
  • 批量处理能力
  • 详细的处理日志

技术分析

节点类型及作用

  • If
  • Code
  • Stickynote
  • Webhook
  • Telegram

复杂度评估

配置难度:
★★★☆☆
维护难度:
★★☆☆☆
扩展性:
★★★★☆

实施指南

前置条件

  • 有效的Gmail账户
  • n8n平台访问权限
  • Google API凭证
  • AI分类服务订阅

配置步骤

  1. 在n8n中导入工作流JSON文件
  2. 配置Gmail节点的认证信息
  3. 设置AI分类器的API密钥
  4. 自定义分类规则和标签映射
  5. 测试工作流执行
  6. 配置定时触发器(可选)

关键参数

参数名称 默认值 说明
maxEmails 50 单次处理的最大邮件数量
confidenceThreshold 0.8 分类置信度阈值
autoLabel true 是否自动添加标签

最佳实践

优化建议

  • 定期更新AI分类模型以提高准确性
  • 根据邮件量调整处理批次大小
  • 设置合理的分类置信度阈值
  • 定期清理过期的分类规则

安全注意事项

  • 妥善保管API密钥和认证信息
  • 限制工作流的访问权限
  • 定期审查处理日志
  • 启用双因素认证保护Gmail账户

性能优化

  • 使用增量处理减少重复工作
  • 缓存频繁访问的数据
  • 并行处理多个邮件分类任务
  • 监控系统资源使用情况

故障排除

常见问题

邮件未被正确分类

检查AI分类器的置信度阈值设置,适当降低阈值或更新训练数据。

Gmail认证失败

确认Google API凭证有效且具有正确的权限范围,重新进行OAuth授权。

调试技巧

  • 启用详细日志记录查看每个步骤的执行情况
  • 使用测试邮件验证分类逻辑
  • 检查网络连接和API服务状态
  • 逐步执行工作流定位问题节点

错误处理

工作流包含以下错误处理机制:

  • 网络超时自动重试(最多3次)
  • API错误记录和告警
  • 处理失败邮件的隔离机制
  • 异常情况下的回滚操作